let playerM = require("../model/player");
let changelistM = require("../model/changelist");
module.exports = {
    // 报名页面
    async index(ctx, index) {
        // console.log(ctx.request.body);
        let userInfo = ctx.request.body;
        let openid = userInfo.openid;
        let randomid = Math.floor(Math.random() * 10000);
        // let avatar = userInfo.headimgurl+"?v="+randomid;
        // let nickname = userInfo.nickname+ randomid;
        let avatar = userInfo.headimgurl;
        let nickname = userInfo.nickname;
        let hasgift = 0;
        try {
            await playerM.findOne({
                where: {
                    openid
                }
            }).then(async instance => {
                if (instance) {
                    // 测试用
                    // await playerM.create({
                    //     openid,
                    //     avatar,
                    //     nickname,
                    //     hasgift,
                    //     randomid
                    // })
                    ctx.body = myU.jsonIfy(2, "你已经报名参加过了!");
                } else {
                    await playerM.create({
                        openid,
                        avatar,
                        nickname,
                        hasgift,
                        randomid
                    })
                    ctx.body = myU.jsonIfy(1, "报名成功!");
                }
            })
        } catch (e) {
            ctx.body = myU.jsonIfy(0, "报名失败!");
        }
    },
    //获取所有用户
    async getAllUser(ctx, next) {
        try {
            let res = await playerM.findAll({
                raw: true
            })
            ctx.body = myU.jsonIfy(1, "获取所有用户成功!", res)
        } catch (e) {
            ctx.body = myU.jsonIfy(0, "获取用户失败!")
        }
    },
    // 得到中奖人员
    async getTarget(ctx, next) {
        try {
            let allUser = await playerM.findAll({
                raw: true,
                where: {
                    hasgift: 0
                }
            });
            let targetUser = await playerM.findAll({
                where: {
                    hasgift: 0
                },
                raw: true,
                limit: 2,
                order: ["randomid"]
            })
            // 如果只剩一个人的话,就不修改他的状态了
            if (targetUser.length > 1) {
                for (let i = 0; i < targetUser.length; i++) {
                    let cur = targetUser[i];
                    await playerM.update({
                        hasgift: 1
                    }, {
                        where: {
                            id: cur.id
                        }
                    })
                }
                // 修改奖品对照表
                await changelistM.create({
                    name1: targetUser[0].nickname,
                    openid1: targetUser[0].openid,
                    name2: targetUser[1] ? targetUser[1].nickname : "",
                    openid2: targetUser[1] ? targetUser[1].openid : ""
                })
            }
            ctx.body = myU.jsonIfy(1, "获取成功", {
                allUser,
                targetUser
            })
        } catch (e) {
            ctx.body = myU.jsonIfy(0, "抽奖失败!")
        }
    }
}